package cn.dnuihyy406.dnuifood_16110100406.controller.foodlist;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.bean.FoodBean;
import cn.dnuihyy406.dnuifood_16110100406.controller.getfood.GetFoodActivity;
import cn.dnuihyy406.dnuifood_16110100406.model.BaseModel;

public class FoodListAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements
        View.OnClickListener{
    private Context mContext;
    private List<FoodBean> foodsList1;
    private LayoutInflater mLayoutInflater;

//    private ShopAdapter.OnRecyclerViewItemClickListenr mOnItemClickListener = null;

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v=mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.food_list_item,viewGroup,false);
//        v.setOnClickListener(this);
        return new FoodListAdapter.ViewHolder(v);
    }
    public FoodListAdapter (Context mContext,List foodsList1,int food_item){
        this.mContext = mContext;
        this.foodsList1=foodsList1;
        mLayoutInflater=LayoutInflater.from(mContext);
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder Holder, final int i) {
            final FoodBean x=foodsList1.get(i);
            if (null==x){
            return;
        }
        final ViewHolder viewHolder= (ViewHolder)Holder;
        viewHolder.price.setText(x.getPrice()+"元");
        viewHolder.intro.setText(x.getIntro());
        viewHolder.foodname.setText(x.getFoodname());
//        String Base_url ="http://172.24.10.175:8080/foodService/";
        Picasso.with(mContext).load(BaseModel.BaseUrl+x.getPic()).into(viewHolder.image);
        viewHolder.itemView.setTag(x);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str = String.valueOf(foodsList1.get(i).getFood_id());
                String str1 = String.valueOf(foodsList1.get(i).getPrice());
                Intent intent=new Intent(mContext, GetFoodActivity.class);
                intent.putExtra("food_id",str);
                intent.putExtra("price",str1);
                mContext.startActivity(intent);
                Toast.makeText(mContext,x.getFoodname(),Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return foodsList1.size();
    }

    @Override
    public void onClick(View view) {
//        if(mOnItemClickListener!=null){
//            //使用getTag方法获取数据
//            mOnItemClickListener.onItemClick(view,view.getTag());
//        }
    }

    //listener   interface
//    public interface OnRecyclerViewItemClickListenr{
//        void onItemClick(View view,Object data);
//    }
//    public void setOItemClickListener(ShopAdapter.OnRecyclerViewItemClickListenr listener) {
//        this.mOnItemClickListener=listener;
//    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView price,foodname,intro;
        ImageView image;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            price= itemView.findViewById(R.id.price);
            foodname = itemView.findViewById(R.id.food_name);
            intro = itemView.findViewById(R.id.intro);
            image = itemView.findViewById(R.id.food_pic);

        }
    }


}